package com.myapplication.zhou.alldemo;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.myapplication.helper.PreferencesHelper;
import com.myapplication.helper.PreferencesHelper1;
import com.myapplication.model.UserInfo;
import com.myapplication.zhou.fragment.PersonInfoFragment;
import com.myapplication.zhou.fragment.SearchFragment;
import com.myapplication.zhou.fragment.ShoppingCartFragment;
import com.myapplication.zhou.fragment.SortTopViewFragment;
import com.myapplication.zhou.fragment.TopViewFragment;

/**
 * @author zhoushikun
 * @see com.ashokvarma.bottomnavigation.BottomNavigationBar.OnTabSelectedListener
 * @see android.content.DialogInterface.OnClickListener
 */
public class MainActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener {

    private BottomNavigationBar bottomNavigationBar;
    private TopViewFragment topViewFragment;
    private PersonInfoFragment personInfoFragment;
    private SearchFragment searchFragment;
    private ShoppingCartFragment shoppingCartFragment;
    private SortTopViewFragment sortTopViewFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        bottomNavigationBar.setTabSelectedListener(this);
       /* FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        initTopViewFragment(fragmentTransaction);
        fragmentTransaction.commit();
*/
    }

    /**
     * 判断sh值是否存在如果不存在，则将其界面设为首页。
     */
    @Override
    protected void onResume() {
        super.onResume();
        String value = getSh1(this, "main", "tag");

        switch (value) {
            case "search":
                onTabSelected(2);
                bottomNavigationBar.selectTab(2);
                break;
            case "topview":
                onTabSelected(0);
                bottomNavigationBar.setTag(0);
                break;
            case "shop":
                if (getSH() == null) {
                    onTabSelected(0);
                    bottomNavigationBar.selectTab(0);
                } else {
                    onTabSelected(3);
                    bottomNavigationBar.setTag(3);
                }
                break;
            case "person":
                if (getSH() == null) {
                    onTabSelected(0);
                    bottomNavigationBar.selectTab(0);
                } else {
                    onTabSelected(4);
                    bottomNavigationBar.setTag(4);
                }
                break;

            case "0000":
                onTabSelected(0);
                bottomNavigationBar.setTag(0);
                break;
        }

    }

    /**
     * @param fragmentTransaction 显示主界面
     */
    public void initTopViewFragment(FragmentTransaction fragmentTransaction) {
        if (topViewFragment != null) {

            fragmentTransaction.show(topViewFragment);
        } else {
            topViewFragment = TopViewFragment.newInstance();
            fragmentTransaction.add(R.id.fragment_main, topViewFragment, "TOP");
            fragmentTransaction.show(topViewFragment);
        }
    }

    /**
     * @param fragmentTransaction init PersonInfoFragment
     */
    public void initPersonInfoFragment(FragmentTransaction fragmentTransaction) {
        if (personInfoFragment != null) {
            fragmentTransaction.show(topViewFragment);
        } else {
            personInfoFragment = PersonInfoFragment.newInstance();
            fragmentTransaction.add(R.id.fragment_main, personInfoFragment, "PERSON");
            fragmentTransaction.show(personInfoFragment);
        }
    }

    /**
     * @param fragmentTransaction 显示搜索界面
     */
    public void initSearchFragment(FragmentTransaction fragmentTransaction) {
        if (searchFragment == null) {
            searchFragment = SearchFragment.newInstance();
            fragmentTransaction.add(R.id.fragment_main, searchFragment, "SEARCH");
            fragmentTransaction.show(searchFragment);
        } else

            fragmentTransaction.show(searchFragment);
    }

    /**
     * @param transaction 显示购物车界面
     */
    public void initShoppingCartFragment(FragmentTransaction transaction) {
        if (shoppingCartFragment == null) {
            shoppingCartFragment = ShoppingCartFragment.newInstance();
            transaction.add(R.id.fragment_main, shoppingCartFragment, "SHOP");
            transaction.show(shoppingCartFragment);
        } else
            transaction.show(shoppingCartFragment);
    }

    /**
     * @param transaction 显示分类界面
     */
    public void initSortTopViewFragment(FragmentTransaction transaction) {
        if (sortTopViewFragment == null) {
            sortTopViewFragment = SortTopViewFragment.newInstance();
            transaction.add(R.id.fragment_main, sortTopViewFragment, "SORT");
            transaction.show(sortTopViewFragment);
        } else
            transaction.show(sortTopViewFragment);
    }

    /**
     * @param transaction 隐藏所有的fragment
     */
    public void hideAllFragment(FragmentTransaction transaction) {
        if (topViewFragment != null) {
            transaction.hide(topViewFragment);
        }
        if (searchFragment != null) {
            transaction.hide(searchFragment);
        }
        if (shoppingCartFragment != null) {
            transaction.hide(shoppingCartFragment);
        }
        if (personInfoFragment != null) {
            transaction.hide(personInfoFragment);
        }
        if (sortTopViewFragment != null) {
            transaction.hide(sortTopViewFragment);
        }
    }

    /**
     * 初始化 BottonNavigationBar
     * {@link BottomNavigationBar}
     */
    private void initView() {
        bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);
        bottomNavigationBar.addItem(new BottomNavigationItem(R.mipmap.iconfont_zhuye02, "商城"))
                .addItem(new BottomNavigationItem(R.mipmap.icon_search02, "搜索"))
                .addItem(new BottomNavigationItem(R.mipmap.iconfont_fenlei02, "分类"))
                .addItem(new BottomNavigationItem(R.mipmap.iconfont_gouwuche02, "购物车"))
                .addItem(new BottomNavigationItem(R.mipmap.icon_my02, "我的商城"))
                .setActiveColor(R.color.bottomcolor)
                .setInActiveColor(R.color.inactivecolor)
                .setBarBackgroundColor(R.color.windowbackground)
                .setMode(BottomNavigationBar.MODE_FIXED)
                .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC)
                .initialise();


    }

    /*@Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        // Necessary to restore the BottomBar's state, otherwise we would
        // lose the current tab on orientation change.
        bottomBar.onSaveInstanceState(outState);
    }*/

    /**
     * 选中后点击事件
     *
     * @param position
     */
    @Override
    public void onTabSelected(int position) {
        switch (position) {
            case 0:
                onSelect(0);
                break;
            case 1:
                onSelect(1);
                break;
            case 2:
                onSelect(2);
                break;
            case 3:
                setSh1(this, "main", "tag", "shop");
                if (null == getSH()) {
                    goToLoginActivity();
                } else
                    onSelect(3);
                break;
            case 4:
                setSh1(this, "main", "tag", "person");
                if (null == getSH()) {
                    goToLoginActivity();
                } else
                    onSelect(0);
                break;
        }

    }

    public void onSelect(int position) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        hideAllFragment(fragmentTransaction);
        switch (position) {
            case 0:
                initTopViewFragment(fragmentTransaction);
                break;
            case 1:
                initSearchFragment(fragmentTransaction);
                break;
            case 2:
                initSortTopViewFragment(fragmentTransaction);
                break;
            case 3:
                if (null == getSH()) {
                    goToLoginActivity();
                } else
                    initShoppingCartFragment(fragmentTransaction);
                break;
            case 4:
                if (null == getSH()) {
                    goToLoginActivity();
                } else
                    initPersonInfoFragment(fragmentTransaction);
                break;
        }
        fragmentTransaction.commit();
    }

    /**
     * 跳转到注册页面
     */
    public void goToLoginActivity() {

        Intent intent = new Intent();
        intent.setClass(this, LoginActivity.class);
        startActivity(intent);
    }

    /**
     * 获取sh的userInfo
     *
     * @return
     */
    public UserInfo getSH() {

        return PreferencesHelper.getUserInfo(this);
    }

    public void setSh1(Context context, String name, String key, String value) {
        PreferencesHelper1.setSharedValue(context, name, key, value);
    }

    public String getSh1(Context context, String name, String key) {
        return PreferencesHelper1.getSharedValue(context, name, key);
    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }

}
